package com.zhuizikejithreeig.cn.threeigkeji.loginfo;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhuizikejithreeig.cn.been.MacErrBeen;
import com.zhuizikejithreeig.cn.been.MesBeen;
import com.zhuizikejithreeig.cn.been.RegisterBeen;
import com.zhuizikejithreeig.cn.content.ContectUtils;
import com.zhuizikejithreeig.cn.content.UriContect;
import com.zhuizikejithreeig.cn.threeigkeji.BasicActivity;
import com.zhuizikejithreeig.cn.threeigkeji.R;
import com.zhuizikejithreeig.cn.utils.GsonUtil;
import com.zhuizikejithreeig.cn.utils.ToastMake;
import com.zhuizikejithreeig.cn.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;


import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.MediaType;


public class RegistActivity extends BasicActivity implements View.OnClickListener {

    private EditText mName1;
    private EditText mName2;
    private Button mName5;
    private EditText mName3;
    private Button mName8;
    private TimerTask wait;
    private int time;
    private ImageView mAgree;
    private boolean b = true;
    private Timer timer = new Timer();
    private TextView mAgreeText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_regist);

        setLeftShow(1, R.mipmap.back);
        getLift_V().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mName1 = (EditText) findViewById(R.id.name1);
        mName2 = (EditText) findViewById(R.id.name2);
        mAgree = (ImageView) findViewById(R.id.agree);
        mAgreeText = (TextView) findViewById(R.id.agree_text);

        mName5 = (Button) findViewById(R.id.name5);
        mName8 = (Button) findViewById(R.id.name8);
        mName3 = (EditText) findViewById(R.id.name3);
//        mName6 = (EditText) findViewById(R.id.name6);
        mName5.setOnClickListener(this);
        mName8.setOnClickListener(this);
        mAgree.setOnClickListener(this);
        mAgreeText.setOnClickListener(this);


        //设置同意协议书
        if (b) {
            mAgree.setSelected(true);
            b = !b;
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            //点击发送验证码
            case R.id.name5:
                final String trim = mName1.getText().toString().trim();
                if (TextUtils.isEmpty(trim)) {
                    ToastMake.showToast(getResources().getString(R.string.string51));
                    return;
                }
                if (trim.length() != 11) {
                    ToastMake.showToast(getResources().getString(R.string.string52));
                    return;
                }
                if (!Utils.isMobileNO(trim)) {

                    return;
                }
                time = 60;
                wait = new TimerTask() {
                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (time <= 0) {
                                    mName5.setEnabled(true);
                                    mName5.setFocusable(true);
                                    mName5.setFocusableInTouchMode(true);
                                    mName5.requestFocus();
                                    mName5.setText(getResources().getString(R.string.string49));
                                    mName1.setEnabled(true);
                                    wait.cancel();
                                } else {
                                    mName5.setEnabled(false);
                                    mName5.setFocusable(false);
                                    mName5.setFocusableInTouchMode(false);
                                    mName5.setText(time + "秒后重获");
                                    mName1.setEnabled(false);
                                }
                                time--;
                            }
                        });
                    }
                };
                if (isConnected()) {
                    sendPostRequest(trim);
                    timer.schedule(wait, 0, 1000);
                } else {
                    ToastMake.showToast(getResources().getString(R.string.string57));
                    wait.cancel();
                    mName5.setEnabled(true);
                    mName5.setFocusable(true);
                    mName5.setFocusableInTouchMode(true);
                    mName5.requestFocus();
                    mName5.setText(getResources().getString(R.string.string49));
                    mName1.setEnabled(true);
                    return;
                }

                break;

            //点击注册
            case R.id.name8:
                if (isConnected()) {
                    regist();
                } else {
                    ToastMake.showToast(getResources().getString(R.string.string57));
                }
                break;
            case R.id.agree:
//                if (b) {
//                    mAgree.setSelected(true);
//                } else {
//                    mAgree.setSelected(false);
//                }
//                b = !b;

                break;
            case R.id.agree_text:
                skipOtherActivity(Agreement.class, false);
                break;
        }
    }


    private void regist() {
        final String phoneNumber = mName1.getText().toString().trim();
        final String passWord = mName2.getText().toString().trim();
        String code = mName3.getText().toString().trim();
//        String secode = mName6.getText().toString().trim();
        if (TextUtils.isEmpty(phoneNumber)) {
            ToastMake.showToast(getResources().getString(R.string.string51));
            return;
        }
        if (TextUtils.isEmpty(code)) {
            ToastMake.showToast(getResources().getString(R.string.string54));
            return;
        }

        if (TextUtils.isEmpty(passWord)) {
            ToastMake.showToast(getResources().getString(R.string.string53));
            return;
        }

        if (!Utils.isMobileNO(phoneNumber)) {
            return;
        }
        if (!Utils.everyPaw(passWord)) {
            return;
        }
        if (b) {
            ToastMake.showToast(getResources().getString(R.string.string68));
            return;
        }
        showProgrees(getResources().getString(R.string.string60));
        HashMap<String, String> map = new HashMap<>();
        map.put("phone", phoneNumber);
        map.put("pwd", passWord);
        map.put("code", code);
        String json = GsonUtil.parseMapToJson(map);
        showProgrees(getResources().getString(R.string.string60));
        OkHttpUtils
                .postString()
                .url(UriContect.REGISTER)
                .content(json)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastMake.showToast(getResources().getString(R.string.string58));
                        hideProgree();
                    }
                    @Override
                    public void onResponse(String response, int id) {
                        hideProgree();
                        RegisterBeen been = GsonUtil.parseJsonToBean(response, RegisterBeen.class);
                        MacErrBeen errBeen=GsonUtil.parseJsonToBean(response, MacErrBeen.class);
                        if (been == null) {
                           if (errBeen!=null){
                               ToastMake.showToast(errBeen.getMsg());
                           }
                            return;
                        }
                        if (!TextUtils.equals(been.getError_code(), "0")) {
                            ToastMake.showToast(been.getMsg());
                            return;
                        } else {
                            if (been.getData().getStatus() != null) {
                                Utils.setString(been.getData().getStatus(), ContectUtils.STATUS);
                            } else {
                                Utils.setString("2", ContectUtils.STATUS);
                            }
                            Utils.setString(phoneNumber, ContectUtils.PHONESAVE);
                            Utils.setString(passWord, ContectUtils.PASSWORLD);
                            skipOtherActivity(BasicInfoActivity.class, true);
                        }
                    }
                });
    }
    private void sendPostRequest(String phone) {
        HashMap<String, String> map = new HashMap<>();
        map.put("phone", phone);
        map.put("tags", "1");
        String json = GsonUtil.parseMapToJson(map);
        OkHttpUtils
                .postString()
                .url(UriContect.GETCHECKCODE)
                .content(json)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                        ToastMake.showToast(getResources().getString(R.string.string58));
                        wait.cancel();
                        mName5.setEnabled(true);
                        mName5.setFocusable(true);
                        mName5.setFocusableInTouchMode(true);
                        mName5.requestFocus();
                        mName5.setText(getResources().getString(R.string.string49));
                        mName1.setEnabled(true);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        MesBeen mesBeen = GsonUtil.parseJsonToBean(response, MesBeen.class);
                        if (mesBeen == null) {
                            wait.cancel();
                            ToastMake.showToast(getResources().getString(R.string.string58));
                            mName5.setEnabled(true);
                            mName5.setFocusable(true);
                            mName5.setFocusableInTouchMode(true);
                            mName5.requestFocus();
                            mName5.setText(getResources().getString(R.string.string49));
                            mName1.setEnabled(true);
                            return;

                        }
                        if (!TextUtils.equals(mesBeen.getError_code(), "0")) {
                            wait.cancel();
                            ToastMake.showToast(mesBeen.getMsg());
                            mName5.setEnabled(true);
                            mName5.setFocusable(true);
                            mName5.setFocusableInTouchMode(true);
                            mName5.requestFocus();
                            mName5.setText(getResources().getString(R.string.string49));
                            mName1.setEnabled(true);
                            return;
                        }
                    }
                });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer!=null) {
            timer = null;
        }
        if (wait!=null){
            wait.cancel();
            wait = null;
        }
    }
}

